package cn.xiangyuanpin.web;

import cn.xiangyuanpin.pojo.Cart;
import cn.xiangyuanpin.pojo.User;
import cn.xiangyuanpin.service.OrderService;
import cn.xiangyuanpin.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OrderServlet extends BaseServlet{

    private OrderService orderService=new OrderServiceImpl();

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       Cart cart=(Cart) req.getSession().getAttribute("cart");
        User user= (User) req.getSession().getAttribute("user");
        if(user==null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        }else  {
           String orderId=orderService.createOrder(cart, user.getId());
           req.setAttribute("orderId", orderId);
            System.out.println("订单号是"+orderId);
            req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
        }
    }
}
